package cn.com.nes.site.module.dao.crm;

import cn.com.nes.common.baiduface.Faceprint;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.dao.BaseDaoImpl;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Repository
public class BaiduFaceDaoImpl extends BaseDaoImpl {
	
	
	public PageData create(String verifyToken, String businessId) {
		PageData res = new PageData();
		res.setRetCode(9);
        res.setRetMessage("保存实名认证结果失败");
        res.setData(new LinkedHashMap<String, Object>());
        
        JdbcTemplate jdbcTemplate = this.getJdbcTemplate_newg();
        TransactionStatus tx = this.begin_newg();
        
		try {
			String sql = "insert into baidu_realface(verify_token, business_id, status) values(?,?,0)";
			int rows = jdbcTemplate.update(sql, verifyToken, businessId);
			if(rows > 0) {
				res.setRetCode(0);
		        res.setRetMessage("OK");
			}
			this.commit_newg(tx);
		} catch (Exception e) {
			this.rollback_newg(tx);
			res.setRetMessage(e.getMessage());
		}
		return res;
	}
	
	public PageData update(Faceprint faceprint) {
		PageData res = new PageData();
		res.setRetCode(9);
        res.setRetMessage("保存实名认证结果失败");
        res.setData(new LinkedHashMap<String, Object>());
        
        NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(this.getJdbcTemplate_newg());
        TransactionStatus tx = this.begin_newg();
        
        try {
			String sql = "update baidu_realface set " +
					"result_detail=:resultDetail, " +
					"result_simple=:resultSimple, " +
					"result_stat=:resultStat, " +
					"idcard_image_front=:idcardImageFront, " +
					"idcard_images_back=:idcardImagesBack, " +
					"face_image=:faceImage, " +
					"status=:status, " +
					"idcard_number=:idcardNumber, " +
					"idcard_name=:idcardName, " +
					"message=:message, " +
					"modifyTime=CURRENT_TIMESTAMP " +
					"where status = 0 and verify_token=:verifyToken";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("resultDetail", faceprint.getResultDetail());
			params.put("resultSimple", faceprint.getResultSimple());
			params.put("resultStat", faceprint.getResultStat());
			params.put("idcardImageFront", faceprint.getIdcardImagesFront());
			params.put("idcardImagesBack", faceprint.getIdcardImagesBack());
			params.put("faceImage", faceprint.getFaceImage());
			params.put("status", faceprint.isSuccess() ? 1 : 2);
			params.put("idcardNumber", faceprint.getIdcardNumber());
			params.put("idcardName", faceprint.getIdcardName());
			params.put("message", faceprint.getMessage());
			params.put("verifyToken", faceprint.getVerifyToken());
			int rows = jdbcTemplate.update(sql, params);
			if(rows > 0) {
				res.setRetCode(0);
		        res.setRetMessage("OK");
			}
			this.commit_newg(tx);
		} catch (Exception e) {
			this.rollback_newg(tx);
			res.setRetMessage(e.getMessage());
		}
        
        return res;
	}
	
	public PageData check(String verifyToken) {
		PageData res = new PageData();
		res.setRetCode(9);
        res.setRetMessage("保存实名认证结果失败");
        res.setData(new LinkedHashMap<String, Object>());
        
        TransactionStatus tx = this.begin_newg();
        
		JdbcTemplate jdbcTemplate = this.getJdbcTemplate_newg();
        try {
			int count = jdbcTemplate.queryForObject("select count(*) from baidu_realface where status = 0 and verify_token=?", new Object[] {verifyToken}, Integer.class);
			if(count > 0) {
				res.setRetCode(0);
			    res.setRetMessage("OK");
			}
			this.commit_newg(tx);
		} catch (DataAccessException e) {
			this.rollback_newg(tx);
			res.setRetMessage(e.getMessage());
		}
        return res;
	}
	
}
